home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 90 / CD Actual 90.iso / Software3D / K-3D / k3d-0.4.2.1 / shaders / k3d_terranbump.sl < prev    next >
Encoding:
Text File  |  2004-07-23  |  2.0 KB  |  73 lines

  1.  
  2.  
  3. #ifdef BMRT
  4. #define snoise(x) (2*(noise(x)-0.5))
  5. #else
  6. /* This is because PRMAN's noise has less range than BMRT's */
  7. #define snoise(x) (2.5*(noise(x)-0.5))
  8. #endif
  9.  
  10. #define DNoise(x) ((2*(point noise(x))) - point(1,1,1))
  11. #define VLNoise(Pt,scale) (snoise(DNoise(Pt)+(scale*Pt)))
  12. #define N_OFFSET 0.7
  13. #define VERY_SMALL 0.0001
  14.  
  15.  
  16.  
  17. displacement k3d_terranbump(float spectral_exp = 0.5;
  18.                 float lacunarity = 2, octaves = 7;
  19.                 float bump_scale = 0.04; float multifractal = 0;
  20.                 float dist_scale = .2; float offset = 0;
  21.                 float sea_level = 0;)
  22. {
  23.   float chaos;
  24.   point Ptexture, tp;
  25.   float l, o, a, i, weight;    /* Loop variables for fBm calc */
  26.   float bumpy;
  27.  
  28.   /* Do all shading in shader space */
  29.   Ptexture = transform("shader", P);
  30.  
  31.   if(multifractal == 0)
  32.     {                /* use a "standard" fBm bump function */
  33.       o = 1;
  34.       l = 1;
  35.       bumpy = 0;
  36.       for(i = 0; i < octaves; i += 1)
  37.     {
  38.       bumpy += o * snoise(l * Ptexture);
  39.       l *= lacunarity;
  40.       o *= spectral_exp;
  41.     }
  42.     }
  43.   else
  44.     {                /* use a "multifractal" fBm bump function */
  45.       /* get "distortion" vector, as used with clouds */
  46.       Ptexture += dist_scale * DNoise(Ptexture);
  47.       /* compute bump vector using MfBm with displaced point */
  48.       o = spectral_exp;
  49.       tp = Ptexture;
  50.       weight = abs(VLNoise(tp, 1.5));
  51.       bumpy = weight * snoise(tp);
  52.       for(i = 1; i < octaves && weight >= VERY_SMALL; i += 1)
  53.     {
  54.       tp *= lacunarity;
  55.       /* get subsequent values, weighted by previous value */
  56.       weight *= o * (N_OFFSET + snoise(tp));
  57.       weight = clamp(abs(weight), 0, 1);
  58.       bumpy += snoise(tp) * min(weight, spectral_exp);
  59.       o *= spectral_exp;
  60.     }
  61.     }
  62.  
  63.   /* get the "height" of the bump, displacing by offset */
  64.   chaos = bumpy + offset;
  65.  
  66.   /* set bump for land masses (i.e., areas above "sea level") */
  67.   if(chaos > sea_level)
  68.     P += (bump_scale * bumpy) * normalize(Ng);
  69.  
  70.   /* Recalculate the surface normal (this is where all the real magic is!) */
  71.   N = calculatenormal(P);
  72. }
  73.